package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.PageDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.enums.HttpEnum;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.utils.LoginThreadLocal;
import com.itheima.reggie.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 菜品及套餐分类(Category)表控制层
 *
 * @author makejava
 * @since 2024-02-01 21:32:16
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    /**
     * 服务对象
     */
    @Resource
    private CategoryService categoryService;

    /**
     * 分类新增
     *
     * @param param
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category param) {
        // 参数校验 isBlack会检查是否为空字符串 isEmpty不会
        if (null == param || StringUtils.isBlank(param.getName())) {
            return R.error(HttpEnum.PARAMETER_NULL.getMsg());
        }
        if (1 != param.getType() && 2 != param.getType()) {
            return R.error(HttpEnum.PARAMETER_ERROR.getMsg());
        }
        // 重复校验
        Category byName = categoryService.getOne(Wrappers.<Category>lambdaQuery()
                .eq(Category::getName, param.getName())
                .last("limit 1"));
        // db中不为空
        if (null != byName) {
            return R.error("该分类已存在");
        }
        // 新增
        param.setCreateTime(LocalDateTime.now());
        param.setCreateUser(LoginThreadLocal.getCurrent());
        param.setUpdateTime(LocalDateTime.now());
        param.setUpdateUser(LoginThreadLocal.getCurrent());
        categoryService.save(param);
        return R.success(param.getType() == 1 ? "新增菜品分类成功!" : "新增套餐分类成功!");
    }


    @GetMapping("/page")
    public R page(PageDto dto) {
        // 参数校验
        if (null == dto) {
            return R.error(HttpEnum.PARAMETER_NULL.getMsg());
        }
        PageUtils.setPage(dto);
        // 分页
        // PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //
        // List<Category> categoryList = categoryService.list(Wrappers.<Category>lambdaQuery()
        //         .orderByAsc(Category::getType)
        //         .orderByAsc(Category::getSort)
        //         .orderByDesc(Category::getUpdateTime));
        // PageInfo<Category> categoryPageInfo = new PageInfo<>(categoryList);
        // return R.success(new HashMap<String, Object>() {{
        //     put("total", categoryPageInfo.getTotal());
        //     put("records", categoryPageInfo.getList());
        // }});

        // MP分页
        Page<Category> categoryPage = categoryService.page(new Page<>(dto.getPage(), dto.getPageSize()), Wrappers.<Category>lambdaQuery()
                .orderByAsc(Category::getType)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime));
        return R.success(categoryPage);
    }


    @PutMapping
    public R update(@RequestBody Category dto) {
        if (null == dto) {
            return R.error(HttpEnum.PARAMETER_NULL.getMsg());
        }
        Category byId = categoryService.getById(dto.getId());
        if (null == byId) {
            return R.error("该id:".concat(dto.getId().toString()).concat("分类在数据库中不存在"));
        }
        dto.setUpdateUser(LoginThreadLocal.getCurrent());
        dto.setUpdateTime(LocalDateTime.now());
        categoryService.updateById(dto);
        return R.success("该分类修改成功");
    }


    /**
     * 分类集合展示
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R typeList(@RequestParam(required = false) Integer type) {
        // if (type == null) {
        //     return R.error(HttpEnum.PARAMETER_ERROR.getMsg());
        // }
        List<Category> categoryList = categoryService.list(Wrappers.<Category>lambdaQuery()
                .eq(null!=type,Category::getType, type)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime));
        return R.success(categoryList);
    }

}

